package com.blq.common.utils;

import com.blq.common.utils.redis.RedisUtils;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 短信发送
 *
 * @author: Blq <br>
 */
public class SendMessageUtils {
    private static final String apikey = "674e4f860aa24ad9b866aa366ece7db5";
    private static final String url = "http://115.28.96.167:8081/";
    private static final String userId = "352135";
//    private static final String autograph = "【慧族科技】";
    private static final String autograph = "【优电易】";
    // 短信宝
    private static final String dxb_url = "https://api.smsbao.com/sms";
    private static final String dxb_user = "cc0731rw";
    private static final String dxb_password = "cc0731RW";

    /**
     * 短信发送主方法
     *
     * @param msgContent
     * @param phone
     */
    public static boolean sendMessage(String msgContent, String phone) {
        String httpArg = "u=" + dxb_user + "&" +
                "p=" + Md5Utils.md5enc(dxb_password) + "&" +
                "m=" + phone + "&" +
                "c=" + Md5Utils.encodeUrlString(msgContent, "UTF-8");
        String result = Md5Utils.request(dxb_url, httpArg);
        if ("0".equals(result)) {
            return true;
        } else {
            if ("30".equals(result)) {
                System.out.println("错误密码");
            } else if ("40".equals(result)) {
                System.out.println("账号不存在");
            } else if ("41".equals(result)) {
                System.out.println("余额不足");
            } else if ("43".equals(result)) {
                System.out.println("IP地址限制");
            } else if ("50".equals(result)) {
                System.out.println("内容含有敏感词");
            } else if ("51".equals(result)) {
                System.out.println("手机号码不正确");
            }
        }
        return false;
    }

    /**
     * 短信发送主方法
     *
     * @param msgContent
     * @param phone
     */
    public static boolean sendMessage2(String msgContent, String phone) {
        Long ts = System.currentTimeMillis();
        Map<String, String> paras = new HashMap<String, String>();
        paras.put("userid", userId); // 用户id
        paras.put("ts", ts.toString());
        paras.put("sign", Md5Utils.md5enc(userId + ts + apikey).toLowerCase());
        paras.put("mobile", phone);
        paras.put("msgcontent", msgContent);
        String result =
                ClineUtil.requestAsHttpPOST(
                        url + "api/sms/send", paras, "utf-8", 10000, 20000);
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> resmap = null;
        try {
            resmap = mapper.readValue(result, Map.class);
            if ("0".equals(resmap.get("code"))) { // 返回成功
                return true;
            } else {
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 发送用户登陆
     *
     * @param code
     * @return
     */
    public static boolean sendLoginCode(String code, String phoneNumber) {
        String message = autograph + "验证码${code}，五分钟内有效。您正在登录，若非本人操作，请勿泄露。";
        message = message.replace("${code}", code);
        return sendMessage(message, phoneNumber);
    }

    /**
     * 发送用户登陆
     *
     * @param code
     * @return
     */
    public static boolean sinTeacher(String code, String phoneNumber) {
        String message = autograph + "验证码${code}，五分钟内有效。您正在注册讲师，若非本人操作，请勿泄露。";
        message = message.replace("${code}", code);
        return sendMessage(message, phoneNumber);
    }

    /**
     * 发送用户登陆
     *
     * @param code
     * @return
     */
    public static boolean teacherLogin(String code, String phoneNumber) {
        String message = autograph + "验证码${code}，五分钟内有效。您正在登讲师端，若非本人操作，请勿泄露。";
        message = message.replace("${code}", code);
        return sendMessage(message, phoneNumber);
    }

    /**
     * 发送用户注册
     *
     * @param code
     * @return
     */
    public static boolean sendRegisterCode(String code, String phoneNumber) {
        String message = autograph + "验证码${code}，五分钟内有效。您正在注册成为新用户，感谢您的支持！";
        message = message.replace("${code}", code);
        return sendMessage(message, phoneNumber);
    }

    /**
     * 发送用户登陆
     *
     * @param code
     * @return
     */
    public static boolean sendAddMessage(String code, String phoneNumber) {
        String message = autograph + "验证码${code}，五分钟内有效。您正在新增就诊卡，若非本人操作，请勿泄露。";
        message = message.replace("${code}", code);
        return sendMessage(message, phoneNumber);
    }

    /**
     * 发送修改密码
     *
     * @param code
     * @return
     */
    public static boolean sendUpdatePwdCode(String code, String phoneNumber) {
        String message = autograph + "验证码${code}，五分钟内有效。您正在修改密码，感谢您的支持！";
        message = message.replace("${code}", code);
        return sendMessage(message, phoneNumber);
    }


    /**
     * @Function: 发送自定义消息 <br>
     * @Author: Blq
     * @Date: 2022/8/3 14:24
     * @Param message:
     * @Param phoneNumber:
     * @Return: boolean
     **/
    public static boolean sendDIY(String message, String phoneNumber) {
        message = autograph + message;
        if (message.contains("${code}")) {
            String code = getCode();
            message = message.replace("${code}",code);
            RedisUtils.setCacheObject(phoneNumber,code, Duration.ofMinutes(5));
        }
        return sendMessage(message, phoneNumber);
    }

    public static boolean sendDIY(String message, String phoneNumber, String phoneSpare) {
        message = autograph + message;
        if (message.contains("${code}")) {
            String code = getCode();
            message = message.replace("${code}",code);
            RedisUtils.setCacheObject(phoneNumber,code, Duration.ofMinutes(5));
            if (StringUtils.isNotBlank(phoneSpare) && !phoneSpare.equals(phoneNumber)) {
                RedisUtils.setCacheObject(phoneSpare,code, Duration.ofMinutes(5));
            }
        }
        if (StringUtils.isNotBlank(phoneSpare) && !phoneSpare.equals(phoneNumber)) {
            sendMessage(message, phoneSpare);
        }
        return sendMessage(message, phoneNumber);
    }

    /**
     * @Function: 获取四位验证码 <br>
     * @Author: Blq
     * @Date: 2022/8/3 14:25
     * @Return: java.lang.String
     **/
    public static String getCode() {
        String code = "";
        Random random = new Random();
        for (int i = 0; i < 4; i++) {
            code += random.nextInt(10);
        }
        return code;
    }
}
